/*
 * This source code is part of TWaver 4.0
 *
 * Serva Software PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 * Copyright 2002 - 2011 Serva Software. All rights reserved.
 */

package demo.sheet.validate;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.Ellipse2D;

import javax.swing.JComponent;
import javax.swing.JTable;
import javax.swing.UIManager;
import javax.swing.border.Border;
import javax.swing.border.EmptyBorder;
import javax.swing.table.TableCellRenderer;

public class ScaleRenderer extends JComponent implements TableCellRenderer{
	
	protected static Border noFocusBorder = new EmptyBorder(1, 1, 1, 1); 
	protected static Border focusBorder = UIManager.getBorder("Table.focusCellHighlightBorder");
	
	private double value;
	
    public void paintComponent(Graphics g) {
    	super.paintComponent(g);
    	
    	Dimension size = this.getSize();
    	
    	Graphics2D g2 = (Graphics2D) g;
    	g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        GradientPaint gp = new GradientPaint(0, 0, Color.BLUE, size.width, 0, Color.YELLOW, true);
        g2.setPaint(gp);
        g2.fillRect(0, 0, size.width, size.height);
    	
        g2.setColor(Color.GREEN);
    	double r = (size.height - 8) / 2.0;
    	int centerX = (int)(size.width * value);
    	int centerY = (int)r;
    	g2.fill(new Ellipse2D.Double(centerX-r, centerY-r+4, r * 2, r * 2));
    }
    
	public Component getTableCellRendererComponent(JTable table, Object value, 
			boolean isSelected, boolean hasFocus, int row, int column) {
    	if(hasFocus){
    		this.setBorder(focusBorder);
    	}else{
    		this.setBorder(noFocusBorder);
    	}
		if(value instanceof Number){
			this.value = ((Number)value).doubleValue();
		}else{
			this.value = 0;
		}
    	this.setToolTipText(value + "");
		return this;
	}
}